<html>
<head><meta charset="utf-8"><title>generic impl for trait with generic. · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/generic.20impl.20for.20trait.20with.20generic.2E.html">generic impl for trait with generic.</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="225712058"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/generic%20impl%20for%20trait%20with%20generic./near/225712058" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/generic.20impl.20for.20trait.20with.20generic.2E.html#225712058">(Feb 09 2021 at 16:03)</a>:</h4>
<p>Hey, is there any way to get this to work? I want <code>Baz</code> to be implemented for everything that implements <code>Bar</code>.</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">trait</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">trait</span><span class="w"> </span><span class="n">Bar</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nc">Foo</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">trait</span><span class="w"> </span><span class="n">Baz</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="c1">// Errors</span>
<span class="k">impl</span><span class="o">&lt;</span><span class="n">B</span>: <span class="nc">Bar</span><span class="o">&lt;</span><span class="n">F</span><span class="o">&gt;</span><span class="p">,</span><span class="w"> </span><span class="n">F</span>: <span class="nc">Foo</span><span class="o">&gt;</span><span class="w"> </span><span class="n">Baz</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">B</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
</code></pre></div>
<p><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=245f7547115de96f2f0e6ab21b0e5c7a">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=245f7547115de96f2f0e6ab21b0e5c7a</a></p>



<a name="225712619"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/generic%20impl%20for%20trait%20with%20generic./near/225712619" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/generic.20impl.20for.20trait.20with.20generic.2E.html#225712619">(Feb 09 2021 at 16:07)</a>:</h4>
<p>I think you have to make <code>T</code> an associated type of Bar or Baz</p>



<a name="225712891"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/generic%20impl%20for%20trait%20with%20generic./near/225712891" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/generic.20impl.20for.20trait.20with.20generic.2E.html#225712891">(Feb 09 2021 at 16:08)</a>:</h4>
<p>this works:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">trait</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">trait</span><span class="w"> </span><span class="n">Bar</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">type</span> <span class="nc">T</span>: <span class="nc">Foo</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">trait</span><span class="w"> </span><span class="n">Baz</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">B</span>: <span class="nc">Bar</span><span class="o">&gt;</span><span class="w"> </span><span class="n">Baz</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">B</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
</code></pre></div>



<a name="225713256"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/generic%20impl%20for%20trait%20with%20generic./near/225713256" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/generic.20impl.20for.20trait.20with.20generic.2E.html#225713256">(Feb 09 2021 at 16:11)</a>:</h4>
<p><span class="user-mention" data-user-id="232545">@Joshua Nelson</span> Thanks, I did realise I could do that, though I didn't implement it as a <code>type</code>, I just went with <code>trait Baz&lt;T: Foo&gt; {}</code> is there any difference/advantage to choosing one over the other?</p>



<a name="225713390"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/generic%20impl%20for%20trait%20with%20generic./near/225713390" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/generic.20impl.20for.20trait.20with.20generic.2E.html#225713390">(Feb 09 2021 at 16:12)</a>:</h4>
<p>Associated types mean you can only implement it for one generic type, like <code>Iterator::Item</code>. Making it generic in the trait means you can have multiple impls, like in <code>From</code></p>



<a name="225730567"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/generic%20impl%20for%20trait%20with%20generic./near/225730567" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/generic.20impl.20for.20trait.20with.20generic.2E.html#225730567">(Feb 09 2021 at 17:59)</a>:</h4>
<p>See, for example, the difference between <code>Deref&lt;Target = T&gt;</code> and <code>AsRef&lt;T&gt;</code>.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>